package org.mybatis.generator.codegen.mybatis3.xmlmapper.elements;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;
import org.mybatis.generator.codegen.mybatis3.ListUtilities;
import org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities;

public class UpdateBySelectiveWithIdsElementGenerator extends AbstractXmlElementGenerator {
    public UpdateBySelectiveWithIdsElementGenerator() {
        super();
    }

    @Override
    public void addElements(XmlElement parentElement) {
        XmlElement answer = new XmlElement("update"); //$NON-NLS-1$

        answer.addAttribute(new Attribute(
                "id", introspectedTable.getUpdateBySelectiveWithIds())); //$NON-NLS-1$


        context.getCommentGenerator().addComment(answer);

        StringBuilder sb = new StringBuilder();

        sb.append("update "); //$NON-NLS-1$
        sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
        answer.addElement(new TextElement(sb.toString()));

        XmlElement dynamicElement = new XmlElement("set"); //$NON-NLS-1$
        answer.addElement(dynamicElement);

        for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable
                .getNonPrimaryKeyColumns())) {
            sb.setLength(0);
            sb.append("record."+introspectedColumn.getJavaProperty());
            sb.append(" != null"); //$NON-NLS-1$
            XmlElement isNotNullElement = new XmlElement("if"); //$NON-NLS-1$
            isNotNullElement.addAttribute(new Attribute("test", sb.toString())); //$NON-NLS-1$
            dynamicElement.addElement(isNotNullElement);

            sb.setLength(0);
            sb.append(MyBatis3FormattingUtilities
                    .getEscapedColumnName(introspectedColumn));
            sb.append(" = "); //$NON-NLS-1$
            sb.append(MyBatis3FormattingUtilities
                    .getParameterClause(introspectedColumn,"record."));
            sb.append(',');

            isNotNullElement.addElement(new TextElement(sb.toString()));
        }

        XmlElement where = new XmlElement("where");

        XmlElement ifIdNotNull = new XmlElement("if");
        ifIdNotNull.addAttribute(new Attribute("test", "list != null")); //$NON-NLS-1$
        ifIdNotNull.addElement(new TextElement(MyBatis3FormattingUtilities
                .getEscapedColumnName(introspectedTable
                        .getPrimaryKeyColumns().get(0)) + " IN "));
        XmlElement forEach = new XmlElement("foreach");
        forEach.addAttribute(new Attribute("collection", "list"));
        forEach.addAttribute(new Attribute("open", "("));
        forEach.addAttribute(new Attribute("close", ")"));
        forEach.addAttribute(new Attribute("separator", ","));
        forEach.addAttribute(new Attribute("item", "item"));
        forEach.addElement(new TextElement("#{item}"));

        ifIdNotNull.addElement(forEach);
        where.addElement(ifIdNotNull);

        answer.addElement(where);
        parentElement.addElement(answer);
    }
}
